#include "../server.hpp"

class EchoServer
{
    private:
        TcpServer _server;
    private:
        void OnConnected(const PtrConnection &conn)
        {
            DBG_LOG("New Connection:%p", conn.get());
        }
        void OnClosed(const PtrConnection &conn)
        {
            // DBG_LOG("Closed Connection:%p", conn.get());
        }
        void OnMessage(const PtrConnection &conn, Buffer *buf)
        {
            // DBG_LOG("%s", (buf->GetReadPosition()));
            // buf->MoveReadOffset(buf->ReadAbleSize());
            // std::string str = "Hello World!";
            std::string str = buf->ReadAsStringAndMove(buf->ReadAbleSize());
            conn->Send(str.c_str(), str.size());
            
            // conn->Shutdown();
        }
    public:
        EchoServer(int port): _server(port)
        {
            // 设置线程数量
            _server.SetThreadCount(2);
            // 设置超时销毁时长
            _server.EnableInactiveRelease(10);
            _server.SetMessageCallBack(std::bind(&EchoServer::OnMessage, this, std::placeholders::_1, std::placeholders::_2));
            _server.SetClosedCallBack(std::bind(&EchoServer::OnClosed, this, std::placeholders::_1));
            _server.SetConnectedCallBack(std::bind(&EchoServer::OnConnected, this, std::placeholders::_1));
        }
        void Start() { _server.Start(); }
};